﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.BLL;
using YouHP.Model.Group;
using YouHP.Model;
using System.Web;
using YouHP.Utility;

namespace YouHP.Pages.Profile
{
    public class GroupList:ProfileBasePage
    {

        protected List<GroupInfo> GroupListByUser;


        protected int _pageSize = 21;
        protected int _pageIndex = 1;
        protected int _pageCount = 0;
        protected int _groupCount;
        protected string PagingString;

        public override void InitPage()
        {
            base.InitPage();
            this.PageKey = PageKeys.Profile_GroupList;
        }


        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        private void HandleGet()
        {
            _pageIndex = RequestTools.GetQueryInt("page", 1);
            GroupListByUser = GroupBLL.Instance.GetAllGroupsByUserId(_pageSize, _pageIndex, ProfileData.Id, out _groupCount);

            _pageCount = Utils.GetPageCount(_groupCount, _pageSize);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PagingString = this.GetPagingString(_pageIndex, _pageCount,
                "/Profile/GroupList.aspx" + query, "page");
        }

        private void HandlePost()
        {
            int formid = RequestTools.GetFormInt("formid", 0);
            int typeid = RequestTools.GetFormInt("typeid", 0);
            int groupid = RequestTools.GetFormInt("groupid", 0);
            int role = RequestTools.GetFormInt("grouprole", -100);
            if (typeid == 1 && formid == 1 && groupid > 0 && role != -100)
            {
                bool succ = false;
                bool succ2 = false;
                bool succ3 = false;
                if (role == 2)//如果是圈主，使圈子无效，并将角色置为已删除
                {
                    succ = GroupBLL.Instance.InvalidateGroup(groupid);
                    succ2 = GroupUserBLL.Instance.UpdateGroupUserRole(groupid, UserData.Id, Constants.RoleDeleted);
                    if (succ && succ2)
                    {
                        this.AddMsgLine("退出成功");
                        this.SetUrl("/User/Group/index.aspx");
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败");
                        this.SetShowBackLink(true);
                        return;
                    }

                }
                else
                {

                    succ3 = GroupUserBLL.Instance.UpdateGroupUserRole(groupid, UserData.Id, Constants.RoleDeleted);
                    if (succ3)
                    {
                        this.AddMsgLine("退出成功");
                        this.SetUrl("/Profile/GroupList.aspx?uid="+ProfileData.Id);
                        this.SetMetaRefresh();
                    }
                    else
                    {
                        this.AddErrLine("操作失败");
                        this.SetShowBackLink(true);
                        return;
                    }
                }


            }
            else
            {
                this.AddErrLine("操作失败");
                this.SetShowBackLink(true);
                return;
            }
        }

        private void HandleCommon()
        {
            this.SetTitle("病友小圈子");
            this.AddLocation("病友小圈子", "");
        }
    }
}
